
const app = getApp();
import {
  queryCouponList,
  receiveCoupon,
} from "../../../../service/jb_coupon_service.js"
import {
  queryWorksCheckList,
} from "../../../../service/jb_activity_service"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      { id: '0', title: '待审核', loaded: false },
      { id: '1', title: '审核通过', loaded: false },
      { id: '2', title: '审核拒绝', loaded: false },
      // { id: 'c', title: '已过期', loaded: false }
    ],
    tabHeight: 0,
    couponType: 0,
    couponList: [

    ],
    overCouponList: [],
    isShowLoading: false,//加载状态出现
    isBottomShow: false,//是否没有更多数据了
    page: 1,
    isFirstEnter: true,
  },
  onLoad() {
    this.scrollTopCache = []; // 记录标签页滚动位置
    const windowHeight = wx.getSystemInfoSync().windowHeight; // 实时获取兼容自定义导航条情况
    this.setData({
      tabHeight: windowHeight
    });


    setTimeout(() => {
      this.setData({
        isFirstEnter: false
      })
    }, 1000)
  },
  onShow() {
    this.queryCouponList('refresh');
  },
  //获取列表
  queryCouponList(type) {
    wx.showLoading();
    queryWorksCheckList(this.data.couponType, this.data.page, 10).then(res => {
      wx.hideLoading();
      if (res.code == 200 && res.data) {
        let arr = (type == 'refresh' ? [] : this.data.couponList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          couponList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.page ? true : false,
        })
        console.log("JJ;" + JSON.stringify(this.data.couponList));
      }
    }).catch(err => {
      wx.hideLoading();

      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.page != 1) {
        this.setData({
          page: this.data.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error("JJLog:queryWorksCheckList", err);
    })
  },

  // 下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      page: 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryCouponList('refresh');
  },
  // 加载更多
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      page: this.data.page + 1
    })
    this.queryCouponList()
  },
  handleScroll(e) {
    // const index = Number(e.currentTarget.dataset.index);
    // const { scrollTop } = e.detail;
    // this.scrollTopCache[index] = scrollTop;
    // this.meetBackTop.setBackTop(scrollTop, `scrollTop[${index}]`);
  },
  handleChange(e) {
    console.log(e);
    const index = e.detail.index || 0;
    console.log("JJLog:index是======:" + index);
    this.setData({
      couponType: index,
    })
    if (!this.data.isFirstEnter) {
      this.setData({
        page: 1,
        isBottomShow: false,
        couponList: [],
      })
      this.queryCouponList('refresh')
    }

    if (!this.data.tabs[index].loaded) {

      this.setData({
        [`tabs[${index}].loaded`]: true,

      });

    }
    // this.meetBackTop.setBackTop(this.scrollTopCache[index], `scrollTop[${index}]`);
  },
  pushToCheckDetail(e) {
    let item = e.currentTarget.dataset.item;
    if (item.modelType != 2) {
      //图文
      app.globalData.worksPreview = item;
      wx.navigateTo({
        url: '/featurePackage/pages/works/worksPreview/worksPreview?action=1' + "&worksId=" + item.id,
      })
    } else {
      //视频
      app.globalData.worksPreview = item;
      wx.navigateTo({
        url: '/featurePackage/pages/works/worksVideoPreview/worksVideoPreview?action=1' + "&worksId=" + item.id,
      })
    }
  },
});
